﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;

namespace AnalysisSystem
{
    class ClassificationStatistics
    {
        public static DataTable statisticsTable = new DataTable();
        private static List<string> wellKindList = new List<string>();
        public static void Statistics()
        {
            CreateClassificationTable();
            SingleWellSummary.RecordData();
            FillLeftForthColumn();
            FillLeftOtherColumn();
            int rowCount = wellKindList.Count * 8;
            FillRightFifthColumn(rowCount, 13, in wellKindList);
            FillRightSeventhColumn(rowCount, 15, in wellKindList);
        }

        private static void CreateClassificationTable()
        {
            statisticsTable.TableName = "6-分类统计";
            string[] leftColName = {"序号（左）", "项目（左）", "参数（左）", "数值（左）", "单位（左）", "井号", "分类（左）" };
            string[] rithtColName = { "序号（右）", "分类（右）", "项目（右）", "参数（右）", "数值（右）", "单位（右）", "占比" };
            string[] lcolSecond = {"生产井数（口）", "工区累产", "单井生产天数", "单井开井时率（%）",
                "单井累产油（万吨）", "单井累产水（万方）", "单井累产气（万方）",
                "单井平均日产油 (总体平均)（吨/天）", "单井平均日产水 (总体平均)（方/天）", "单井平均日产气 (总体平均)（万方/天）"};
            string[] lcolThirdPart1 = { "产气", "产水", "产油"};
            String[] lcolThirdPart2 = { "最大", "最小", "平均", "中位数" };
            string[] lcolFifthPart1 = { "口", "亿方", "万方", "万吨" };
            string[] lcolFifthPart2 = { "天", "%", "吨", "方", "万方", "吨", "方", "万方" };
            string[] rcolThird = { "井数", "累产", "井均产" };
            string[] rcolForth = { "产气量", "产水量", "产油量" };
            string[] rcolSixth = { "口", "亿方", "万方", "万吨", "万方/天", "方/天", "吨" };
            //创建列，加入列名
            for(int i=0; i<16; i++)
            {
                if (i < 7) statisticsTable.Columns.Add(leftColName[i]);
                else if (i > 8) statisticsTable.Columns.Add(rithtColName[i - 9]);
                else statisticsTable.Columns.Add();
            }
            AddBlankRow(46);
            for(int col = 0; col < 5; col ++)
            {
                if (col == 0) FillLeftFirstColumn();
                if (col == 1) FillLeftSecondColumn(in lcolSecond);
                if (col == 2) FillLeftThirdColumn(in lcolThirdPart1, in lcolThirdPart2);
                if (col == 4) FillLeftFifthColumn(in lcolFifthPart1, in lcolFifthPart2);
            }
            wellKindList = GetWellKind();
            int rightRowsNum = wellKindList.Count * 8;
            AddBlankRow(rightRowsNum);
            for(int col = 9; col < 15; col ++)
            {
                if (col == 9) FillRightFirstColumn(rightRowsNum, col);
                if (col == 10) FillRightSecondColumn(rightRowsNum, col, in wellKindList);
                if (col == 11) FillRightThirdColumn(rightRowsNum, col, in rcolThird);
                if (col == 12) FillRightForthColumn(rightRowsNum, col, in rcolForth);
                if (col == 14) FillRightSixthColumn(rightRowsNum, col, in rcolSixth);
            }
        }

        private static void AddBlankRow(int rowNum)
        {
            int rowCount = statisticsTable.Rows.Count;
            if(rowNum > rowCount)
            {
                for (int i = 0; i < rowNum - rowCount; i++)
                    statisticsTable.Rows.Add();
            }
        }

        private static List<string> GetWellKind()
        {
            List<string> wellKindList = new List<string>();
            for(int i=0; i<OleDbMethod.dataTables[0].Rows.Count; i++)
            {
                string wellKind = OleDbMethod.dataTables[0].Rows[i][4].ToString();
                if (!wellKindList.Contains(wellKind))
                    wellKindList.Add(wellKind);
            }
            return wellKindList;
        }     

        private static List<double> GetThisWellKindData(in DataTable data)
        {
            List<double> list = new List<double>();
            double[] sumData = new double[3];
            double[] aveData = new double[3];
            string[] sumName = { "累计产气（万方）", "累计产水（方）", "累计产油（吨）" };
            string[] aveName = { "平均日产气（万方/天）", "平均日产水（方/天）", "平均日产油（吨/天）" };
            list.Add(Math.Round((double)data.Rows.Count, 0));
            for(int i=0; i<data.Rows.Count; i++)
            {
                for(int j=0; j<3; j++)
                {
                    sumData[j] += Convert.ToDouble(data.Rows[i][sumName[j]]);
                    aveData[j] += Convert.ToDouble(data.Rows[i][aveName[j]]);
                }
            }
            for(int j=0; j<3; j++)
            {
                list.Add(Math.Round(sumData[j] / 10000, 4));
            }
            for(int j=0; j<3; j++)
            {
                double value = aveData[j] / list[0];
                list.Add(Math.Round(value, 2));
            }
            return list;
        }

        private static void FillLeftFirstColumn()
        {
            int col = 0;
            for (int row = 0; row < 46; row++)
            {
                int index = 0;
                if (row == 0)
                {
                    index = 1;
                    statisticsTable.Rows[row][col] = index;
                }
                if (row == 2)
                {
                    index = 2;
                    statisticsTable.Rows[row][col] = index;
                }
                if (row > 5 && (row - 6) % 5 == 0)
                {
                    index = (row - 6) / 5 + 3;
                    statisticsTable.Rows[row][col] = index;
                }
            }
        }

        private static void FillLeftSecondColumn(in string[] text)
        {
            int col = 1;
            for(int row = 0; row < 46; row ++)
            {
                if (row == 0)
                {                    
                    statisticsTable.Rows[row][col] = text[0];
                }
                if (row == 2)
                {                    
                    statisticsTable.Rows[row][col] = text[1];
                }
                if (row > 5 && (row - 6) % 5 == 0)
                {
                    int index = (row - 6) / 5 + 2;
                    statisticsTable.Rows[row][col] = text[index];
                }
            }
        }

        private static void FillLeftThirdColumn(in string[] text1, in string[] text2)
        {
            int col = 2;
            for(int row = 2; row < 46; row ++)
            {
                if (row == 2) statisticsTable.Rows[row][col] = text1[0];
                if(row == 3) statisticsTable.Rows[row][col] = text1[1];
                if(row == 4) statisticsTable.Rows[row][col] = text1[2];
                if(row > 5)
                {
                    int index = (row - 6) % 5;
                    if(index != 4) statisticsTable.Rows[row][col] = text2[index];
                }
            }
        }

        private static void FillLeftForthColumn()
        {
            int col = 3;
            for(int row=0; row<46; row++)
            {
                if (row == 0) statisticsTable.Rows[row][col] = OleDbMethod.dataTables[0].Rows.Count;
                if (row == 2) statisticsTable.Rows[row][col] = Math.Round(SingleWellSummary.allSum[2] / 10000, 2);
                if (row == 3) statisticsTable.Rows[row][col] = Math.Round(SingleWellSummary.allSum[1] / 10000, 2);
                if (row == 4) statisticsTable.Rows[row][col] = Math.Round(SingleWellSummary.allSum[0] / 10000, 2);
                if(row > 5)
                {
                    int index = (row - 6) / 5;
                    int flag = (row - 6) % 5;
                    if (flag != 4) statisticsTable.Rows[row][col] = Math.Round(SingleWellSummary.singleWellData[index * 4 + flag], 2);
                }
            }
        }

        private static void FillLeftFifthColumn(in string[] text1, in string[] text2)
        {
            int col = 4;
            for(int row = 0; row<46; row ++)
            {
                if (row == 0) statisticsTable.Rows[row][col] = text1[0];
                if (row == 2) statisticsTable.Rows[row][col] = text1[1];
                if (row == 3) statisticsTable.Rows[row][col] = text1[2];
                if (row == 4) statisticsTable.Rows[row][col] = text1[3];
                if(row > 5)
                {
                    int index = (row - 6) / 5;
                    int flag = (row - 6) % 5;
                    if (flag != 4) statisticsTable.Rows[row][col] = text2[index];
                }
            }
        }

        private static void FillLeftOtherColumn()
        {
            string[] selectKey = {"生产天数（天）", "开井时率（%）",
                "累计产油（吨）", "累计产水（方）", "累计产气（万方）",
                "平均日产油（吨/天）", "平均日产水（方/天）", "平均日产气（万方/天）"};
            for (int row = 6; row < 46; row++)
            {
                int flag = (row - 6) % 5;
                int index = (row - 6) / 5;
                if(flag == 0 || flag == 1)
                {
                    double keyValue = Convert.ToDouble(statisticsTable.Rows[row][3]);
                    for (int i = 0; i < SingleWellSummary.singleWellSumTable.Rows.Count; i++)
                    {
                        double value = Convert.ToDouble(SingleWellSummary.singleWellSumTable.Rows[i][selectKey[index]]);
                        if (keyValue.Equals(value))
                        {
                            string wellName = SingleWellSummary.singleWellSumTable.Rows[i]["井号"].ToString();
                            string wellKind = SingleWellSummary.singleWellSumTable.Rows[i]["分类"].ToString();
                            statisticsTable.Rows[row][5] = wellName;
                            statisticsTable.Rows[row][6] = wellKind;
                        }
                    }
                }
            }
        }

        private static void FillRightFirstColumn(int rowCount, int col)
        {
            for(int row=0; row<rowCount; row++)
            {
                if(row % 8 == 0)
                {
                    int index = row / 8 + 1;
                    statisticsTable.Rows[row][col] = index;
                }
            }
        }

        private static void FillRightSecondColumn(int rowCount, int col, in List<string> wellKind)
        {
            for (int row = 0; row < rowCount; row++)
            {
                if (row % 8 == 0)
                {
                    int index = row / 8;
                    statisticsTable.Rows[row][col] = wellKind[index];
                }
            }
        }

        private static void FillRightThirdColumn(int rowCount, int col, in string[] text)
        {
            for(int row = 0; row < rowCount; row++)
            {
                int flag = row % 8;
                if (flag == 0) statisticsTable.Rows[row][col] = text[0];
                if (flag == 1) statisticsTable.Rows[row][col] = text[1];
                if (flag == 4) statisticsTable.Rows[row][col] = text[2];
            }
        }

        private static void FillRightForthColumn(int rowCount, int col, in string[] text)
        {
            for(int row = 0; row < rowCount; row++)
            {
                int flag = row % 8;
                if (flag != 0 && flag != 7)
                {
                    int index = (flag - 1) % 3;
                    statisticsTable.Rows[row][col] = text[index];
                }
            }
        }

        private static void FillRightFifthColumn(int rowCount, int col, in List<string> data)
        {
            DataTable table = new DataTable();
            List<double> valueList = new List<double>();
            for (int row = 0; row < rowCount; row ++)
            {
                int flag = row % 8;
                int index = row / 8;              
                if (flag == 0)
                { 
                    string keyString = data[index];
                    var selectTable = from p in SingleWellSummary.singleWellSumTable.AsEnumerable()
                                      where p.Field<string>("分类").Equals(keyString)
                                      select p;
                    table = selectTable.CopyToDataTable<DataRow>();
                    valueList = GetThisWellKindData(in table);
                }  
                if(flag != 7) statisticsTable.Rows[row][col] = valueList[flag];
            }
        }

        private static void FillRightSixthColumn(int rowCount, int col, in string[] text)
        {
            for(int row = 0; row < rowCount; row++)
            {
                int flag = row % 8;
                if (flag != 7) statisticsTable.Rows[row][col] = text[flag];
            }
        }

        private static void FillRightSeventhColumn(int rowCount, int col, in List<string> data)
        {
            //顺序为气、水、油
            double[] sumData = new double[3];
            for(int i=0, j=2; i<3; i++, j--)
            {
                sumData[i] = SingleWellSummary.allSum[j] / 10000;
            }        
            for (int row = 0; row < rowCount; row++)
            {
                int flag = row % 8;              
                if(flag == 1 || flag == 2 || flag == 3)
                {
                    double value = Convert.ToDouble(statisticsTable.Rows[row][13]);
                    statisticsTable.Rows[row][col] = Math.Round(Convert.ToDouble(value / sumData[flag-1] * 100), 2);
                }
            }
        }
    }
}
